package com.cloudptt.wp;


import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.func.message.NotifyListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.ui.call.CallActivity;
import com.mcptt.http.operator.IHttpAddrOpCb;
//import com.cloudptt.wp.ui.call.CallActivity;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

/**
 * 继承Application
 *
 * @author admin
 *
 */
public class SysApplication extends Application implements NotifyListener
{
	public static final String APPID = "appid=1442820561";//0x55ffb1d1
	public static String THIS_FILE = "SysApplication";
	public static String APP_KEY = "ptt/wuping";

	private Product product = null;
	private IHttpAddrOpCb addrUptListener = null;

	@Override
	public void onCreate()
	{
		super.onCreate();
		Log.d(THIS_FILE,"onCreate");

	}


	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
		Log.d(THIS_FILE,"onLowMemory");
	}
	public void onTrimMemory(int level)
	{
		super.onTrimMemory(level);
		Log.d(THIS_FILE,"onTrimMemory level = " + level);


	};

	@Override
	public void onTerminate()
	{
		Log.d(THIS_FILE,  "onTerminate");

		if(product != null){
			product.doExit();
			product = null;
		}
//		SysApplication app = ((SysApplication) getApplicationContext());
//		Product product = app.getProduct();
//		if(product != null)
//		{
//			product.doExit();
//		}
//
//		ScreenManager.getScreenManager().popAllActivity();
//		SystemClock.sleep(2000);
//		System.exit(0);

		super.onTerminate();
	}


	public static final String GROUP_CREATE_RESULT_ACTION = "org.hytera.create_group_ACTION";
	public void creatProduct()
	{
		Log.d(THIS_FILE,  "creatProduct");
		if(product == null){
			this.product = new Product(this,ProductConst.ProductType_NAPP_RADIO,ProductConst.Product_Simple);
		}
		this.product.setLogStatus();
		this.product.setPCallClass(CallActivity.class,null);
		this.product.setNotifyListener(this);
//		String devId = getDeviceId();
//		this.product.getSysConfig().setDevIdBeforLogin(devId);

		Intent createintent = new Intent(GROUP_CREATE_RESULT_ACTION);
		sendBroadcast(createintent);
	}

//	private String getDeviceId()
//	{
//		TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//		String id = tm.getDeviceId();
//		Log.d(THIS_FILE,  "getDeviceId id = " + id);
//		return id;
//	}

	public Product getProduct() {
		return product;
	}

	public void showWarning(String msg){
		Intent intent = new Intent();
		intent.setAction("android.intent.action.WARNING");
		intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra("msg", msg);
		startActivity(intent);
	}


	@Override
	public void receiveNotify(String msg) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE, "BlackList application msg = "+msg);
		showWarning(msg);
	}
//	@Override
//	public void receiveOnlineMsg()
//	{
//		Log.d(THIS_FILE, "receiveOnlineMsg");
//		if(product != null
//				&& product.getOperator(ProductConst.Func_Addr) != null)
//		{
//			SysConfig config = product.getSysConfig();
//			AddrOperator addrOperator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
//			addrOperator.loadAddress(config,addrUptListener);
//		}
//	}

	public void setAddrUptListener(IHttpAddrOpCb addrUptListener) {
		this.addrUptListener = addrUptListener;
	}
}
